package com.study.zjh.ioswheeldemo.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.study.zjh.ioswheeldemo.R;
import com.study.zjh.ioswheeldemo.utils.IosWheelDesc;
import com.study.zjh.ioswheeldemo.utils.IosWheelId;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class SelectorSingleWindow<T> implements View.OnClickListener {
    private Activity mActivity;
    private PopupWindow selectPhoto;
    private TextView btn_myinfo_cancel;
    private TextView btn_myinfo_sure;
    private LinearLayout ll_root;
    private IosWheelView wv_single;
    private List<T> datas;
    private List<String> desc=new ArrayList<>();
    private List<Integer> ids = new ArrayList<>();
    private String value;
    private int code=1;

    public SelectorSingleWindow(Activity activity, List<T> datas) {
        this.mActivity = activity;
        this.datas=datas;
    }

    public void showSelectPopupWind() throws IllegalAccessException {
        selectPhoto = new PopupWindow(selectPhotoView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //点击空白处消失
        selectPhoto.setOutsideTouchable(true);
        selectPhoto.setAnimationStyle(R.style.popwin_anim);
        //给popupWindow设置背景
        selectPhoto.setBackgroundDrawable(new BitmapDrawable());
        backgroundAlpha(0.5f);
        selectPhoto.showAtLocation(new TextView(mActivity), Gravity.BOTTOM, 0, 0);
        //添加pop窗口关闭事件
        selectPhoto.setOnDismissListener(new poponDismissListener());
    }


    public void dissPopupWindow(){
      if(selectPhoto!=null&&selectPhoto.isShowing()){
          selectPhoto.dismiss();
      }
    }
    private View selectPhotoView() throws IllegalAccessException {
        View view = View.inflate(mActivity, R.layout.select_single, null);
        btn_myinfo_cancel = (TextView) view.findViewById(R.id.btn_myinfo_cancel);
        btn_myinfo_sure = (TextView) view.findViewById(R.id.btn_myinfo_sure);
        ll_root = (LinearLayout)view.findViewById(R.id.ll_root);
        wv_single = (IosWheelView)view.findViewById(R.id.wv_single);
        btn_myinfo_cancel.setOnClickListener(this);
        btn_myinfo_sure.setOnClickListener(this);
        initData();
        return view;
    }

    private void initData() throws IllegalAccessException {
        setData();
        // 初始化仿IOS滚轮效果的数据
        // 在这里可以设置滚轮的偏移量
        wv_single.setOffset(1);
        //设置每一个Item中的数据 mArrayList中装着的是一堆String字符串
        wv_single.setItems(desc);
        wv_single.setSeletion(0);
        value=desc.get(0);
        wv_single.setOnWheelViewListener(new IosWheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                //selectedIndex当前高亮的位置
                //item当前高亮的位置的内容
                value=item;
                code=selectedIndex;
                Log.i("selectedIndex","selectedIndex==="+selectedIndex);
            }
        });
    }

    private void setData() throws IllegalAccessException {
        desc.clear();
        ids.clear();
        for(T t:datas){
            Class clazz = t.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for(Field field:fields){
                //通过注解的方式获取展示的内容和id
                if(field.getAnnotation(IosWheelDesc.class)!=null){
                    field.setAccessible(true);
                    String de = (String)field.get(t);
                    desc.add(de);
                }

                if(field.getAnnotation(IosWheelId.class)!=null){
                    field.setAccessible(true);
                    int id=field.getInt(t);
                    ids.add(id);
                }
            }
        }
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_myinfo_cancel:
                if(onChancedDayListener!=null){
                    onChancedDayListener.onCancleClick();
                }
                selectPhoto.dismiss();
                break;
            case R.id.btn_myinfo_sure:
                if(onChancedDayListener!=null){
                    onChancedDayListener.onSureClick(value,ids.get(code-1));
                }
                selectPhoto.dismiss();
                break;
        }
    }

    class poponDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            backgroundAlpha(1f);
        }
    }
    private OnChancedDayListener onChancedDayListener;

    public void setOnChancedDayListener(OnChancedDayListener onChancedDayListener){
        this.onChancedDayListener = onChancedDayListener;
    }

    public interface OnChancedDayListener {
        void onSureClick(String str1, int code);
        void onCancleClick();
    }
}
